// 难度按钮切换
document.querySelectorAll('.difficulty-btn').forEach(btn => {
	btn.addEventListener('click', function() {
		document.querySelectorAll('.difficulty-btn').forEach(b => b.classList.remove('active'));
		this.classList.add('active');
	});
});

// 删除选项
document.querySelectorAll('.delete-option').forEach(btn => {
	btn.addEventListener('click', function() {
		if (document.querySelectorAll('.option-item').length > 2) {
			this.closest('.option-item').remove();
			updateOptionLabels();
		} else {
			alert('至少需要保留两个选项！');
		}
	});
});

// 添加选项
document.querySelector('.add-option').addEventListener('click', function() {
	const optionsContainer = document.getElementById('optionsContainer');
	const optionCount = optionsContainer.children.length;
	const newLabel = String.fromCharCode(65 + optionCount);

	const newOption = document.createElement('div');
	newOption.className = 'option-item';
	newOption.innerHTML = `
                <div class="option-label">${newLabel}.</div>
                <input type="text" class="option-input" placeholder="请输入选项内容">
                <button type="button" class="delete-option">删除</button>
            `;

	optionsContainer.appendChild(newOption);
	newOption.querySelector('.delete-option').addEventListener('click', function() {
		if (document.querySelectorAll('.option-item').length > 2) {
			this.closest('.option-item').remove();
			updateOptionLabels();
		} else {
			alert('至少需要保留两个选项！');
		}
	});
	const answerOptions = document.querySelector('.answer-options');
	const newRadio = document.createElement('label');
	newRadio.className = 'answer-option';
	newRadio.innerHTML = `
                <input type="radio" name="correct" value="${newLabel}">
                <span>${newLabel}</span>
            `;
	answerOptions.appendChild(newRadio);
});

function updateOptionLabels() {
	const options = document.querySelectorAll('.option-item');
	options.forEach((option, index) => {
		const label = String.fromCharCode(65 + index);
		option.querySelector('.option-label').textContent = label + '.';
	});

	const answerOptions = document.querySelector('.answer-options');
	answerOptions.innerHTML = '';
	options.forEach((option, index) => {
		const label = String.fromCharCode(65 + index);
		const radio = document.createElement('label');
		radio.className = 'answer-option';
		radio.innerHTML = `
                    <input type="radio" name="correct" value="${label}"${index === 0 ? ' checked' : ''}>
                    <span>${label}</span>
                `;
		answerOptions.appendChild(radio);
	});
}

document.getElementById('questionForm').addEventListener('submit', function(e) {
	e.preventDefault();
	const formData = {
		type: document.getElementById('questionType').value,
		subject: document.getElementById('subject').value,
		difficulty: document.querySelector('.difficulty-btn.active').dataset.difficulty,
		score: document.getElementById('score').value,
		content: document.querySelector('.question-content').value,
		options: Array.from(document.querySelectorAll('.option-input')).map(input => input.value),
		correctAnswer: document.querySelector('input[name="correct"]:checked').value
	};

	console.log('Form Data:', formData);
	alert('题目保存成功！');
});

//返回按钮
document.querySelector('.return-btn').addEventListener('click', function() {
	if (confirm('确定要返回吗？未保存的内容将会丢失')) {
		window.location.href = "testmanage.html";
	}
});